/*
 * StraightLineMarker.java
 *
 * Created on 30 April 2007, 21:41
 *
 */

package gui.markers;

import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.Color;

import gui.CreateGui;

/**
 * Line Marker Class
 * @author Slawek Wojcicki
 */
public class StraightLineMarker extends FigureMarker {
    
    /**
     * Creates a new instance of StraightLineMarker
     */
    public StraightLineMarker() {
        
        super( "StraightLine" + instance_counter++ );
        this.line = null;
        this.startPoint = null;
    }
    
    public void drawFigure(Graphics2D g, boolean dragged) {
        
        g.setXORMode( Color.white );
        if(!dragged)
            g.setPaintMode();
        g.draw( line );
    }
    
    private java.awt.geom.Line2D.Double newLine2D(double endX, double endY) {
        
        return new java.awt.geom.Line2D.Double(startPoint.getX(),
                startPoint.getY(), endX, endY);
    }
    
    public void mouseDragged(int x, int y, Graphics2D g) {
        
        g.setXORMode(Color.white);
        g.draw( line ); // ???
        line = newLine2D(x, y);
        g.draw( line );
    }
    
    public void mousePressed(int x, int y, Graphics2D g) {
        
        startPoint = new Point2D.Double(x, y);
        line = newLine2D(x, y);
        g.draw( line );
    }
    
    public boolean mouseReleased(int x, int y, Graphics2D g) {
        
        g.setXORMode(Color.white);
        g.draw( line );
        line = newLine2D(x, y);
        g.setPaintMode();
        g.draw( line );
        return ( line.getP1().distance(line.getP2()) > 0.0 );
    }
    
    public boolean contains(Point2D p) {
        
        return line.intersects( p.getX() - 1.0, p.getY() - 1.0, 3.0, 3.0);
    }
    
    public void move(double x, double y) {
        
        line.setLine( line.getX1() + x, line.getY1() + y,
                line.getX2() + x, line.getY2() + y );
    }
    
    private Point2D startPoint;
    private Line2D.Double line;
    private static int instance_counter = 1;
}
